import axios from 'axios'

// 无需token的URL
const annoUrls = ['/login'];

let token = null;
try {
  token = JSON.parse(sessionStorage.getItem('user')).token;
} catch (e) {
  token = null;
}

const request = axios.create({
  // baseURL: 'http://localhost:8888/api/',
  baseURL: 'http://test-vehicle.construn.cn/vehicle/user/user',
  timeout: 5000
});

// 添加拦截器
request.interceptors.request.use(config => {
  let index = annoUrls.findIndex(item => item == config.url);
  if (index != -1) {
    return config;
  }
  if (!token) {
    throw new Error('登录已过期');
  }
  config.headers.token = token;
  return config;
})

export default request;